package com.xu.cloud.common.advice.exception;

import com.xu.cloud.common.constant.OrderConstant;
import com.xu.cloud.common.exception.XuCloudException;
import com.xu.cloud.common.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * 全局异常处理类
 *
 * @author persimmon
 * @since 2023/4/22
 */
@Order(OrderConstant.XU_CLOUD_EXCEPTION_HANDLER_ORDER)
@Slf4j
@RestControllerAdvice
public class XuCloudExceptionHandlerAdvice {

    @ExceptionHandler(XuCloudException.class)
    @ResponseStatus(code = HttpStatus.OK)
    public Result<?> handlerCommonException(XuCloudException xuCloudException) {
        String code = xuCloudException.getCode();
        String message = xuCloudException.getMessage();
        log.error("捕获到xuCloudException通用异常, message: ", xuCloudException);
        return Result.fail(code, message, null);
    }
}
